package yang.learn.controller;

import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import yang.learn.entity.ChatRoom;
import yang.learn.entity.ChatRoomFriend;
import yang.learn.entity.User;
import yang.learn.service.ChatRoomFriendService;
import yang.learn.service.ChatRoomService;
import yang.learn.service.UserService;

import java.util.*;

@Controller
@RequestMapping("/chat/room")
public class ChatRoomController {

    @Autowired(required = false)
    private ChatRoomService chatRoomService;
    @Autowired(required = false)
    private ChatRoomFriendService chatRoomFriendService;
    @Autowired(required = false)
    private UserService userService;

    @ResponseBody
    @RequestMapping("list")
    public ModelAndView list(User user){
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("redirect:/");
        List<ChatRoom> chatRoomList = chatRoomService.queryAll(user);
        modelAndView.addObject(chatRoomList);
        return modelAndView;
    }

    @ResponseBody
    @RequestMapping("user_list")
    public Map userList(HttpServletRequest request){
        Map<String, Object> chatRoomMap = new HashMap<>();
        User user = new User();
        int user_id = Integer.parseInt(String.valueOf(request.getSession().getAttribute("user_id")));
        user.setUser_id(user_id);
        List<ChatRoom> chatRoomList = chatRoomService.queryAll(user);
        chatRoomMap.put("code",0);
        chatRoomMap.put("msg","");
        chatRoomMap.put("count",chatRoomList.size());
        chatRoomMap.put("data",chatRoomList);
        return chatRoomMap;
    }

    @ResponseBody
    @RequestMapping("manage")
    public ModelAndView manage(HttpServletRequest request){
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("redirect:/");
        HttpSession session = request.getSession();
        session.setAttribute("chat_id",request.getParameter("chat_id"));
        return modelAndView;
    }

    @RequestMapping("add")
    public ModelAndView query(@RequestBody ChatRoom chatRoom, HttpServletRequest request) {
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("chatroom/add");
        int user_id = Integer.parseInt(String.valueOf(request.getSession().getAttribute("user_id")));
        chatRoom.setChat_master_id(user_id);
        chatRoomService.add(chatRoom);
        System.out.println("聊天室创建成功！");
        return modelAndView;
    }

    @RequestMapping("del")
    public ModelAndView del(@RequestBody ChatRoom chatRoom,HttpServletRequest request) {
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("chatroom/del");
        int user_id = Integer.parseInt(String.valueOf(request.getSession().getAttribute("user_id")));
        chatRoom.setChat_master_id(user_id);
        ChatRoom query = chatRoomService.queryId(chatRoom);
        if(query==null){//聊天室不存在
            System.out.println("聊天室不存在！");
        }else if(query.getChat_master_id() != user_id){//不是主人
            System.out.println("您没有权限删除该聊天室！");
        }else{
            chatRoomService.del(chatRoom);
            System.out.println("聊天室删除成功！");
        }
        return modelAndView;
    }

    @RequestMapping("friend/add")
    public ModelAndView addFriend(@RequestBody ChatRoomFriend chatRoomFriend, ModelAndView modelAndView,HttpServletRequest request){
        modelAndView.setViewName("/chatroom/friend/add");
        System.out.println(chatRoomFriend);
        User user = new User();
        user.setUser_account(chatRoomFriend.getUser_account());
        User queryAccount = userService.queryAccount(user);
        ChatRoom chatRoom = new ChatRoom();
        chatRoom.setChat_id(chatRoomFriend.getChat_id());
        ChatRoom query = chatRoomService.queryId(chatRoom);
        chatRoomFriend.setUser_name(queryAccount.getUser_name());
        int user_id = Integer.parseInt(String.valueOf(request.getSession().getAttribute("user_id")));
        if(queryAccount==null){//不存在
            System.out.println("用户不存在！");
        }else if(query == null){//不存在
            System.out.println("聊天室不存在！");
        }else if(query.getChat_master_id() != user_id){//不是管理员
            System.out.println("您没有权限删除该聊天室！");
        }else{
            chatRoomFriendService.add(chatRoomFriend);
            System.out.println("邀请成功！");
        }
        return modelAndView;
    }
    @RequestMapping("friend/del")
    public ModelAndView delFriend(@RequestBody ChatRoomFriend chatRoomFriend, ModelAndView modelAndView){
        modelAndView.setViewName("/chatroom/friend/del");
        chatRoomFriendService.del(chatRoomFriend);
        return modelAndView;
    }

}
